//
//  HistoryRecond.m
//  HMSupplier
//
//  Created by haohao on 2017/7/7.
//  Copyright © 2017年 haohao. All rights reserved.
//

#import "HistoryRecond.h"

#define CITYMARIN 20

#define recondWidth (KWidthSCREEN - 5*CITYMARIN) / 4

#define recondHeght 30

#define heghtMarin 10


@interface HistoryRecond ()

NS_STRONG NSArray *recondArray;

NS_STRONG UIView *cityView;

NS_STRONG UILabel *recentLabel;

@end

@implementation HistoryRecond

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView {
    _recondArray = [MGAccountTool getRecond];
    
    //搜索记录
    _cityView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidthSCREEN, 2000)];
    _cityView.backgroundColor = ke5e5e5;
    [self addSubview:_cityView];
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]};//指定字号
    CGRect rect = [@"搜索记录" boundingRectWithSize:CGSizeMake(self.width - 30, 0)/*计算高度要先指定宽度*/ options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil];
    _recentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, heghtMarin, rect.size.width, rect.size.height)];
    _recentLabel.font = [UIFont systemFontOfSize:15.0f];
    _recentLabel.text = @"搜索记录";
    
    NSInteger MAX = 0;
    if (_recondArray.count != 0) {
        [self addSubview:_recentLabel];

        
        for (int i = 0; i < _recondArray.count; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.tag = 100 + i;
            button.frame = CGRectMake((i % 4 + 1) * CITYMARIN + (i % 4) * recondWidth, CGRectGetMaxY(_recentLabel.frame) + (i / 4 + 1) * heghtMarin + (i / 4 - 1) * recondHeght, recondWidth, recondHeght);
            [button setTitle:_recondArray[i] forState:UIControlStateNormal];
            [button setBackgroundColor:[UIColor whiteColor]];
            [button setTitleColor:k999999 forState:UIControlStateNormal];
            button.layer.cornerRadius = 5;
            button.clipsToBounds = YES;
            button.layer.borderColor = [UIColor colorWithHexString:@"c9c9c9"].CGColor;
            [button addTarget:self action:@selector(getRecond:) forControlEvents:UIControlEventTouchUpInside];
            [_cityView addSubview:button];
            

            MAX = CGRectGetMaxY(button.frame);
        }
    }
    
    _cityView.frame = CGRectMake(0, CGRectGetMaxY(_recentLabel.frame) + heghtMarin, KWidthSCREEN, MAX + heghtMarin);
    if (MAX == 0) {
        self.frame = CGRectMake(0, 0, KWidthSCREEN, 0);

    } else {
        self.frame = CGRectMake(0, 0, KWidthSCREEN, CGRectGetMaxY(_cityView.frame) + heghtMarin);

    }
}

- (void)getRecond:(UIButton *)sender{
    if ([_delegate respondsToSelector:@selector(returnSearchString:)]) {
        [_delegate returnSearchString:sender.titleLabel.text];
    }
}

- (void)refreshData {
    NSArray *subView = [self subviews];
    for (int i = 0; i < subView.count; i++) {
        UIView *view = subView[i];
        [view removeFromSuperview];
    }
    [self setupSubView];
//    _recondArray = [MGAccountTool getRecond];
//    NSInteger MAX = 0;
//        for (int i = 0; i < _recondArray.count; i++) {
//            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//            button.tag = 100 + i;
//            button.frame = CGRectMake((i % 4 + 1) * CITYMARIN + (i % 4) * recondWidth, CGRectGetMaxY(_recentLabel.frame) + (i / 4 + 1) * heghtMarin + (i / 4 - 1) * recondHeght, recondWidth, recondHeght);
//            [button setTitle:_recondArray[i] forState:UIControlStateNormal];
//            [button setBackgroundColor:[UIColor whiteColor]];
//            [button setTitleColor:k999999 forState:UIControlStateNormal];
//            button.layer.cornerRadius = 5;
//            button.clipsToBounds = YES;
//            button.layer.borderColor = [UIColor colorWithHexString:@"c9c9c9"].CGColor;
//            [button addTarget:self action:@selector(getRecond:) forControlEvents:UIControlEventTouchUpInside];
//            [_cityView addSubview:button];
//            
//            
//            MAX = CGRectGetMaxY(button.frame);
//        }
//    
//    _cityView.frame = CGRectMake(0, CGRectGetMaxY(_recentLabel.frame) + heghtMarin, KWidthSCREEN, MAX);
//    
//    self.frame = CGRectMake(0, 0, KWidthSCREEN, CGRectGetMaxY(_cityView.frame) + heghtMarin);
}

@end
